package yls.pro.ess.ui.activity;

import java.util.ArrayList;
import java.util.List;

import yls.pro.ess.R;
import yls.pro.ess.dal.model.consumption;
import yls.pro.ess.ui.adapter.CommonAdapter;
import yls.pro.ess.ui.adapter.ViewHolder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;


public class ConsumptionActivity extends ActionBarActivity {

	private List<consumption> mListItems;
	private PullToRefreshListView mPullRefreshListView;
	private CommonAdapter<consumption> mAdapter;
	private ListView lvShow = null;
	private int y = 0; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_consumption);

		mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
		mPullRefreshListView.setMode(Mode.BOTH);
		// 下拉刷新时的提示文本设置
		mPullRefreshListView.getLoadingLayoutProxy(true, false).setLastUpdatedLabel("下拉刷新");
		mPullRefreshListView.getLoadingLayoutProxy(true, false).setPullLabel("");
		mPullRefreshListView.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在刷新");
		mPullRefreshListView.getLoadingLayoutProxy(true, false).setReleaseLabel("放开以刷新");
		// 上拉加载更多时的提示文本设置
		mPullRefreshListView.getLoadingLayoutProxy(false, true).setLastUpdatedLabel("上拉加载");
		mPullRefreshListView.getLoadingLayoutProxy(false, true).setPullLabel("");
		mPullRefreshListView.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载...");
		mPullRefreshListView.getLoadingLayoutProxy(false, true).setReleaseLabel("放开以加载");
		// 添加 一个下拉刷新事件
		mPullRefreshListView
				.setOnRefreshListener(new OnRefreshListener<ListView>() {

					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						if (refreshView.isHeaderShown()) {
							new GetHeaderDataTask().execute();
						} else {
							// 得到上一次滚动条的位置，让加载后的页面停在上一次的位置，便于用户操作
							//y = mListItems.size();
							new GetBottomDataTask().execute();
							
						}
					}
				});
		lvShow = mPullRefreshListView.getRefreshableView();
		mListItems = initData();
		mAdapter = new CommonAdapter<consumption>(ConsumptionActivity.this,mListItems,R.layout.consumption_item) {

			@Override
			public void convert(ViewHolder holder, consumption t) {
				// TODO Auto-generated method stub
				holder.setText(R.id.consumption_time, t.getTime());
				holder.setText(R.id.consumption_type, t.getType());
				holder.setText(R.id.consumption_text, t.getText());
				holder.setText(R.id.consumption_money, t.getMoney());
			}
		};
		lvShow.setAdapter(mAdapter);
	}

	/*
	 * 功能 ： 下拉刷新时的事件处理
	 */
	private class GetHeaderDataTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
			}
			return "";
		}

		@Override
		protected void onPostExecute(String result) {
			initDataxia();
			mAdapter.notifyDataSetChanged();
			// 停止刷新
			mPullRefreshListView.onRefreshComplete();
			super.onPostExecute(result);
		}
	}

	/*
	 * 功能：　上拉加载更多时的事件处理
	 */
	private class GetBottomDataTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
			}
			return "";
		}

		@Override
		protected void onPostExecute(String result) {
			initDataup();
			mAdapter.notifyDataSetChanged();
			// 停止刷新
			mPullRefreshListView.onRefreshComplete();
			// 设置滚动条的位置 -- 加载更多之后，滚动条的位置应该在上一次划到的位置
			y=lvShow.getFirstVisiblePosition();
			lvShow.setSelection(y+1);
			super.onPostExecute(result);
		}
	}
	
	private List<consumption> initData(){
		List<consumption> list=new ArrayList<consumption>();
		for(int i=0;i<5;i++){
			consumption con=new consumption();
			con.setCardId("循环"+i);
			con.setMoney("Money"+i);
			con.setText("消费"+i);
			con.setTime("时间time"+i);
			con.setType("类别"+i);
			list.add(con);
		}
		return list;
	}
	private void initDataxia(){
		for(int i=0;i<5;i++){
			consumption con=new consumption();
			con.setCardId("循环下拉"+i);
			con.setMoney("Money下拉"+i);
			con.setText("消费下拉"+i);
			con.setTime("时间time下拉"+i);
			con.setType("类别下拉"+i);
			mListItems.add(0, con);
		}
	}
	private void initDataup(){
		for(int i=0;i<5;i++){
			consumption con=new consumption();
			con.setCardId("循环上拉"+i);
			con.setMoney("Money上拉"+i);
			con.setText("消费上拉"+i);
			con.setTime("时间time上拉"+i);
			con.setType("类别上拉"+i);
			mListItems.add(con);
		}
	}
}

